// constants
const CLASS_ID = Components.ID("{1C0E8D56-B661-40d0-AE4D-CA012FADF176}");
const CLASS_NAME = "MyLocation Component";
const CONTRACT_ID = "@rsdn.ru/janus/mylocation;1";

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

//class constructor
function MyLocation()
{
	this.wrappedJSObject = this;
};

//class definition
MyLocation.prototype =
{
	GetMyLocation : function()
	{
        var wrk = Components.classes["@mozilla.org/windows-registry-key;1"]
                            .createInstance(Components.interfaces.nsIWindowsRegKey);
        wrk.open(wrk.ROOT_KEY_CURRENT_USER,
                 "Software\\Rsdn\\Janus",
                 wrk.ACCESS_READ);
                 //alert(wrtk.toString());
        var appPath = wrk.readStringValue("AppPath");
        appPath =appPath.replace("Janus.exe","");
        appPath =appPath.replace("Janus.EXE","");
        wrk.close();
        //alert(appPath);
    var localFile = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
                localFile.initWithPath(
                appPath
		 //"x:\\user_alex_new\\dot_net\\janus-codeplex\\Sourses\\Janus\\bin\\Debug_X86\\" //__LOCATION__;
         );
         return localFile;
	},
	
	  // this must match whatever is in chrome.manifest!
	  classID: CLASS_ID,
	 
	  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIMyLocation]),
	 
	//QueryInterface: function(iid)
	//{
	//	if (!iid.equals(Components.interfaces.nsISupports))
	//		throw Components.results.NS_ERROR_NO_INTERFACE;
	//	return this;
	//}

};

//class factory
var MyLocationFactory =
{
	createInstance: function (outer, iid)
	{
		if (outer != null)
			throw Components.results.NS_ERROR_NO_AGGREGATION;
		return (new MyLocation()).QueryInterface(iid);
	}
};

//module definition (xpcom registration)
var MyLocationModule =
{
	_firstTime: true,

	registerSelf: function(compMgr, fileSpec, location, type)
	{
		if (this._firstTime)
		{
			this._firstTime = false;
			throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
		};
		compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
		compMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, fileSpec, location, type);
	},

	unregisterSelf: function(compMgr, location, type)
	{
		compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
		compMgr.unregisterFactoryLocation(CLASS_ID, location);
	},

	getClassObject: function(compMgr, cid, iid)
	{
		if (!iid.equals(Components.interfaces.nsIFactory))
			throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

		if (cid.equals(CLASS_ID))
			return MyLocationFactory;

		throw Components.results.NS_ERROR_NO_INTERFACE;
	},

	canUnload: function(compMgr)
	{
		return true;
	}
};

// module initialization
// When the application registers the component, this function	is called.
function NSGetModule(compMgr, fileSpec)
{
	return MyLocationModule;
}

/**
* XPCOMUtils.generateNSGetFactory was introduced in Mozilla 2 (Firefox 4, SeaMonkey 2.1).
* XPCOMUtils.generateNSGetModule was introduced in Mozilla 1.9 (Firefox 3.0).
*/
if (XPCOMUtils.generateNSGetFactory)
    var NSGetFactory = XPCOMUtils.generateNSGetFactory([MyLocation]);
else
    var NSGetModule = XPCOMUtils.generateNSGetModule([MyLocation]);